//
//  CZWelcomeViewController.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/13.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {
// MARK: 懒加载图片
    
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var headImageView: UIImageView = {
    let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        view.layer.cornerRadius = 45
       view.layer.masksToBounds = true
        
        if let url =  CZUserAccountViewModel.shareUserAccount.userAccount?.avatar_large{
        view.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
    return view
    }()
    //信息label
    private lazy var messageLabel: UILabel = {
    let label = UILabel()
        
    label.font = UIFont.systemFontOfSize(14)
    
    label.textColor = UIColor.darkGrayColor()

        if let name = CZUserAccountViewModel.shareUserAccount.userAccount?.name{
        label.text = name + "欢迎回来"
        }else
        {
        label.text = "欢迎回来"
        }

    return label
    }()
    
        override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

      setupUI()
    }
    //设置姿势图，和约束
    private func setupUI(){
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
   //设置动画效果
    private func startAnimation(){
    self.headImageView.snp_updateConstraints { (make) -> Void in
        make.top.equalTo(view).offset(100)
        }
    messageLabel.alpha = 0
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //重新加载数据
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            UIView.animateWithDuration(1, animations: { () -> Void in
                self.messageLabel.alpha = 1
                }, completion: { (_) -> Void in
                   NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
            })
        }
    
    }
    
    
    
    
}
